package com.ht.modules.system.util;

import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;

import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class ExportTemplateUtil {

    public static void exportTemplateExcel(Map<String, Object> map, String fileName, String templateName,  HttpServletResponse response) throws IOException {
        TemplateExportParams params = new TemplateExportParams("exportTemplate/" + templateName);
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        //导出数据
        try {
            fileName = new String(fileName.getBytes(), StandardCharsets.ISO_8859_1);
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            OutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new IOException("导出excel失败，请联系管理员！");
        }
    }
    public static Workbook exportTemplateExcel1(Map<String, Object> map, String templateName){
        TemplateExportParams params = new TemplateExportParams("exportTemplate/"+templateName);
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        return workbook;
    }
}
